package com.haha;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import static com.haha.ByteBufferUtil.debugAll;

public class TestByteBufferExam {
    public static void main(String[] args) {
        ByteBuffer source = ByteBuffer.allocate(32);
        source.put("HELLO,WORLD\nI'm zhangsan\nHo".getBytes(StandardCharsets.UTF_8));
        split(source);
        source.put("w are you?\n".getBytes(StandardCharsets.UTF_8));
        split(source);
    }
    public static void split(ByteBuffer source){
        source.flip();
        for (int i = 0; i < source.limit(); i++) {
            if(source.get(i)=='\n'){
                int length = i+1-source.position();
                ByteBuffer buffer = ByteBuffer.allocate(length);
                for (int j=0 ;j<length;j++){
                   buffer.put(source.get());

                }
                debugAll(buffer);
            }
        }
        source.compact();
    }
}
